package de.voichie.android.spike.database;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class QuotesMain extends Activity {
	MyDBAdapter db = new MyDBAdapter(this);
	EditText txtEditText;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.databaselayout);
		// Capture our button from layout
		Button setButton = (Button) findViewById(R.id.go);
		Button getButton = (Button) findViewById(R.id.genRan);
		// Register the onClick listener with the implementation above
		setButton.setOnClickListener(mAddListener);
		getButton.setOnClickListener(mAddListener);
	}

	// Create an anonymous implementation of OnClickListener
	private OnClickListener mAddListener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.go:
				db.open();
				long id = 0;
				// do something when the button is clicked
				try {
					txtEditText = (EditText) findViewById(R.id.Quote);
					db.insertQuote(txtEditText.getText().toString());

				} catch (Exception ex) {
					Context context = getApplicationContext();
					CharSequence text = ex.toString() + "ID = " + id;
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}
				
				
				
				try {
//					String text = db.getMovieURLForTitleRow("Blade 2");
//					String text = db.getRowIDForMovieTitle("Blade 2");
					String text = db.getRowString(1);
					txtEditText.setText(text);
				}catch (Exception ex) {
					Context context = getApplicationContext();
					CharSequence text = ex.toString() + "ID = " + id;
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}
				db.close();
				break;

			}
		}
	};
}
